package com.stupidzhe.vbreview.service.impl;

import com.stupidzhe.vbreview.bean.PageBean;
import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.dao.ClassMapper;
import com.stupidzhe.vbreview.dao.StudentMapper;
import com.stupidzhe.vbreview.domain.Class;
import com.stupidzhe.vbreview.domain.Student;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.ClassService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    private final ClassMapper classMapper;

    private final StudentMapper studentMapper;

    public ClassServiceImpl(ClassMapper classMapper, StudentMapper studentMapper) {
        this.classMapper = classMapper;
        this.studentMapper = studentMapper;
    }

    @Override
    public PageBean<Class> getClassByPage(Integer pageNum, Integer teacherId, String keyword) {
        if (pageNum < 1) pageNum = 1;
        PageBean<Class> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        List<Class> res = classMapper.getPageContent(pageBean.getStart(), pageBean.getSize(), keyword, teacherId);
        pageBean.setRowCount(classMapper.getCount(keyword, teacherId));
        pageBean.setContent(res);
        return pageBean;
    }

    @Override
    public ResultBean addClass(String className, Teacher teacher) {
        Class c = new Class();
        c.setClassNum(className);
        c.setTeacherId(teacher.getUserId());
        try {
            classMapper.insert(c);
            return new ResultBean(true, "添加成功");
        } catch (Exception e) {
            return new ResultBean(false, "添加失败:错误代码1");
        }
    }

    @Override
    public Class selectClass(Integer classId) {
        List<Student> students = studentMapper.selectByClassId(classId);
        Class c = classMapper.selectByPrimaryKey(classId);
        c.setStudents(students);
        return c;
    }

    @Override
    public List<Class> getClassByTeacherId(Integer teacherId) {
        return classMapper.selectByTeacherId(teacherId);
    }

    @Override
    @Transactional
    public ResultBean editClass(Integer classId, String className) {
        if (className.trim().equals("")) {
            return new ResultBean(false, "班级名称不能为空");
        }
        Class c = classMapper.selectByClassName(className);
        if (c != null && c.getClassNum().equals(className)) {
            return new ResultBean(true, "与修改前一致");
        }
        if (c != null) {
            return new ResultBean(false, "班级名称已存在");
        }
        c = classMapper.selectByPrimaryKeyForUpdate(classId);
        if (c == null) {
            return new ResultBean(false, "班级编号不存在");
        }
        c.setClassNum(className);
        try {
            classMapper.updateByPrimaryKey(c);
        } catch (Exception e) {
            return new ResultBean(false, "更新失败");
        }
        return new ResultBean(true, "更新成功");
    }
}
